/**
 *	人员管理
 *	@auth liangpingzheng
 *	@date 2018-02-27
 */
layui.use(['form', 'layer', 'laydate', 'table', 'laytpl', 'upload'], function () {

    //声明变量
    var layer = parent.layer === undefined ? layui.layer : top.layer
            , table = layui.table
            , form = layui.form
            , layedit = layui.layedit
            , laydate = layui.laydate
            , laytpl = layui.laytpl
            , upload = layui.upload
            , $ = layui.$;


    //调用API接口渲染TABLE(方法级)
    var tableIns = table.render({
        elem: "#tableList"
        , url: "/manager/dict"
        , method: 'post'
        , cellMinWidth: 100
        , page: {
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
            , curr: 1
            , groups: 10
            , first: '首页'
            , last: '尾页'
        }
        , height: "full-125"
        , limit: 20
        , limits: [20, 30, 40]
        , even: true //开启隔行背景
        , cols: [[
                {field: 'id', width: '12%', title: '编号', align: 'center'}
                , {field: 'label', width: '12%', title: '标签', align: 'center'}
                , {field: 'code', width: '15%', title: '编码', align: 'center'}
                , {field: 'group_name', width: '15%', title: '字典组', align: 'center'}
                , {field: 'add_time', width: '15%', title: '添加时间', align: 'center'}
                , {field: 'updatedt', width: '15%', title: '操作', align: 'center', templet: function (d) {
                        str = '<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>';
                        str += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>';
                        return str;
                    }}
            ]]
    });



    //监听工具条
    table.on('tool(tableList)', function (obj) {
        var data = obj.data
                , layEvent = obj.event
                , tr = obj.tr;
        if (layEvent === 'del') {
            broderID = data.id;
            layer.confirm('确认删ID为“' + broderID + '”的除标签么？', function (index) {
                $.ajax({
                    url: "/manager/dict/drop",
                    dataType: "json",
                    type: "POST",
                    data: {"id": data.id},
                    beforeSend: function () {
                        layer.msg('正在操作。。。', {
                            icon: 16
                            , shade: 0.01
                            , time: 0
                        });
                    },
                    success: function (res) {
                        if (res.success) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                layui.table.reload("tableList", {
                                    url: '/manager/dict',
                                    where: {
                                        keyword: $(".searchVal").val(), //搜索的关键字
                                        dict_group_id: $(".dict_group_id").val()}
                                });
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });

            });
        } else if (layEvent === 'edit') {
            var index = layui.layer.open({
                type: 2,
                content: "/manager/dict/edit?id=" + data.id,
                area: ["470px", "500px"],
                success: function (layero, index) {
                    //TODO...
                }
            });
        }
        ;
    });
    //添加
    $(".btnAdd").click(function () {
        edit(0);
    });

    //添加或编辑
    function edit(id, data) {
        var title = '新增人员';
        var index = layui.layer.open({
            title: title,
            type: 2,
            area: ["470px", "500px"],
            content: "/manager/dict/edit",
            success: function (layero, index) {
                //TODO...
            }
        });
    }
    ;

//搜索功能
    $(".search_btn").on("click", function () {
        table.reload("tableList", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                keyword: $(".searchVal").val(), //搜索的关键字
                dict_group_id: $(".dict_group_id").val()

            }
        })
    });

    form.on('select(dict_group_id)', function (data) {
        table.reload("tableList", {
            page: {
                curr: 1
            },
            where: {
                keyword: $(".searchVal").val(),
                dict_group_id: data.value
            }
        })
    });


//监听提交
    form.on('submit(submitForm)', function (data) {
        var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.2});
        $.post(aUrl, data.field, function (data) {
            if (data.success) {
                layer.close(index);
                layer.msg("保存成功！");
                parent.layer.closeAll('iframe');
                //刷新父页面
                parent.layui.table.reload("tableList", {
                    url: '/manager/dict',
                    where: {
                        keyword: $(".searchVal").val(), //搜索的关键字
                        dict_group_id: $(".dict_group_id").val()}
                });
                return false;
            } else {
                layer.close(index);
                layer.msg(data.msg);
            }
        }, 'json');
        return false;
    });


});